<?php

namespace App\Http\Controllers\API;

use App\Models\Cart;
use App\Models\Color;
use App\Models\Config;
use App\Models\Goods;
use App\Models\Order;
use App\Repositories\ArticleRepository;
use App\ZL\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class OrdersAPIController extends ApiBaseController
{
    protected $model_name = 'App\Models\Order';
    protected $model_load_array = [
    ];
    protected $search_field_array = [
        'type','status'
    ];

    public function __construct()
    {
    }

    public function authLimit(&$model)
    {
        $user = Auth::user();
        $res = [['user_id', $user?$user->id:0]];
        return $res;
    }

    public function beforeStore($id, $request)
    {
        $user_id = Auth::id();
        $order = Order::find($id);
        $goods_data = $request['goods_data'];
        if(is_string($goods_data))
            $goods_data = json_decode($goods_data, true);

        $all_price = 0;
        $all_number = 0;
        foreach ($goods_data as $v) {
            $cart = Cart::where([
                ['user_id',$user_id],
                ['color_id',$v['color_id']],
                ['goods_id',$v['goods_id']],
            ])->first();
            if($cart){
                $cart->number -= $v['number'];
                $all_price += $cart->goods->price*$v['number'];
                $all_number += $v['number'];
                $order->goods()->attach($cart->goods_id, ['color_id' => $cart->color_id, 'number' => $v['number'], 'price' => $cart->goods->price]);
                $cart->save();
                if($cart->number<=0){
                    $cart->delete();
                }
            }else{
                $goods = Goods::find($v['goods_id']);
                $all_price += $goods->price*$v['number'];
                $all_number += $v['number'];
                $order->goods()->attach($goods->id, ['color_id' => $v['color_id'], 'number' => $v['number'], 'price' => $goods->price]);
            }
        }
        $start_time = Config::where('type', "order_start_date")->value('value');
        $end_time = Config::where('type', "order_end_date")->value('value');
        if(strtotime($start_time)<=time() && strtotime($end_time)>=time()){
            $order->type = 2;
        }
        $order->user_id = Auth::id();
        $order->created_date = date('Y-m-d');
        $order->price = $all_price;
        $order->number = $all_number;
        $order->customer_id = $order->customer_id?$order->customer_id:$order->user_id;
        $order->save();
        return [
            'status' => 1,
            'msg' => 'ok',
            'data' => $order,
        ];
    }


    public function _after_get($data)
    {
        $data->load('goods');
        $data->load('user');
        $data->load('customer');

        $data = $data->toArray();
        foreach ($data as &$v) {
            foreach ($v['goods'] as &$good) {
                $good['color'] = Color::find($good['pivot']['color_id']);
            }
        }
        return $data;
    }

    public function _after_find(&$data)
    {
        $data->user;
        $data->goods;
        $data->customer;
        $data = $data->toArray();

        foreach ($data['goods'] as &$good) {
            $good['color'] = Color::find($good['pivot']['color_id']);
        }
    }
}
